package com.panda.utils;

import com.panda.pojo.Block;
import com.panda.pojo.Element;
import com.panda.pojo.StringBlock;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class OutputUtils {


    //将一个变量输出在这里
    private static final Pattern op = Pattern.compile("([$]?[a-zA-Z_][a-zA-Z_0-9]*([\n ]*\\.[\n ]*[a-zA-Z_][a-zA-Z_0-9]*)*)[ \n]*<<(__output_[a-zA-Z][a-zA-Z0-9]*_[0-9]+)");

    private static final Pattern sp = Pattern.compile("(__string_[a-zA-Z][a-zA-Z0-9]*_[0-9]+)<<(__output_[a-zA-Z][a-zA-Z0-9]*_[0-9]+)");

    //右侧的一律视为变量,左侧的则为需要输入值的内容
    static String transformOutput(Element element, String s, List<ParseUtils.AfterParseScript> afterParseScripts) {


        if ("".equals(s)) {
            return s;
        }


        Matcher matcher;


        List<Block> blocks = element.getSubBlocks();

        matcher = sp.matcher(s);

        while (matcher.find()) {

            String r = matcher.group();

            String o = matcher.group(2);

            String st = matcher.group(1);

            for (Block block : blocks) {

                if (o.equals(block.getId())) {

                    //由于字符串 左右会有 '' 或者 ""  所以这里加上 __string__ 最后最后连带消除__string__'  '__string__
                    ((StringBlock) block).setText("__string__" + st + "__string__");

                    break;
                }

            }


            //替换掉输出
            s = s.replace(r, "");

        }


        matcher = op.matcher(s);

        while (matcher.find()) {

            String o = matcher.group(3);
            String st = matcher.group(1);

            afterParseScripts.add(() -> {

                Map<String, Integer> params = element.getScope().getAllParams();

                for (Block block : blocks) {
                    if (o.equals(block.getId())) {

                        Integer integer = params.get(st);

                        if (integer == null) {

                            integer = Integer.MAX_VALUE;

                            if (st.contains(".")) {

                                for (String key : params.keySet()) {

                                    if (st.startsWith(key + ".")) {
                                        Integer v = params.get(key);
                                        if (v < integer) {
                                            integer = v;
                                        }
                                    }

                                }

                            }

                            if (integer == Integer.MAX_VALUE) {
                                integer = 0;
                            }

                        }


                        String ps = "";

                        //进多层深度
                        for (int i = 0; i < integer; i++) {
                            ps += "$parent.";
                        }

                        ((StringBlock) block).setText("{{" + ps + st + "}}");
                        break;
                    }
                }
            });


            //移除变量输出
            s = s.replace(matcher.group(), "");

        }


        return s;

    }


}
